package com.authine.cloudpivot.web.api.controller;

import com.authine.cloudpivot.engine.api.facade.BizObjectFacade;
import com.authine.cloudpivot.engine.api.model.runtime.BizObjectCreatedModel;
import com.authine.cloudpivot.engine.api.model.runtime.SelectionValue;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.service.DemandUpdateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

@Api(value = "需求修改数据留痕接口", tags = "需求修改相关接口")
@RestController
@RequestMapping("/api/demandUpdate")
@Slf4j
public class DemandUpdateController extends BaseController {

    @Autowired
    private DemandUpdateService demandUpdateService;

    @ApiOperation(value = "店检需求变更留痕" )
    @GetMapping("/djxqUpdate")
    public boolean djxqUpdate(String id,String updateId){
        String schemaCode = "DJXQ";
        BizObjectFacade bizObjectFacade = getBizObjectFacade();
        BizObjectCreatedModel djxqData = bizObjectFacade.getBizObject("DJXQ", id);
        BizObjectCreatedModel djxqbgData = bizObjectFacade.getBizObject("DJXQBG", updateId);

        Map<String, Object> djxq = djxqData.getData();
        Map<String, Object> djxqbg = djxqbgData.getData();
        BigDecimal sort = BigDecimal.ZERO;

        List<Map<String, Object>> bgMaps = new ArrayList<>();
        //系统过滤字段
        List<String> sysKeys = Arrays.asList("modifiedTime","modifier","ownerDeptId","sequenceNo","sequenceStatus","createdTime","id","workflowInstanceId","owner","createdDeptId","ownerDeptQueryCode","name","creater");
        //过滤字段
        List<String> delKeys = Arrays.asList("zfStandard","qcStandard","djxq","subjectName");
        //隐藏字段
        List<String> hidKeys = Arrays.asList("projectNo","djxqId","executor","state","subjectPerson","finishedTime","subjectEndTime","persons");
        //选人控件字段
        List<String> userKeys = Arrays.asList();
        //子表字段
        List<String> sheetKeys = Arrays.asList("djxqbgStore");
        //关联单选的文本
        List<String> selectKeys = Arrays.asList("project_name");

        //子表过滤字段
        List<String> sheetSysKey = Arrays.asList("id","parentId","modifier","sortKey","modifiedTime");

        Set<String> storeCodes = new HashSet<>();
        for (String key : djxqbg.keySet()) {
            Map<String, Object> bgMap = new HashMap<>();
            if(sysKeys.contains(key)||delKeys.contains(key)||hidKeys.contains(key)){
                continue;
            }
            boolean save = false;
            Object djxqValue = djxq.get(key);
            Object djxqbgValue = djxqbg.get(key);
            if(sheetKeys.contains(key)){
                //子表
                List<Map> m1 = (List<Map>) djxq.get("djxqStore");
                List<Map> m2 = (List<Map>) djxqbg.get("djxqbgStore");
                //需求子表
                a : for (Map map2 : m2) {
                    String map2Code = map2.get("storeCode").toString();
                    boolean has = true;
                    //需求变更子表
                    for (Map map1 : m1) {
                        String map1Code = map1.get("storeCode").toString();
                        if(map1Code.equals(map2Code)){
                            has = false;
                            for (Object map1Key : map1.keySet()) {
                                if(sheetSysKey.contains(map1Key)){
                                    continue;
                                }
                                for (Object map2Key : map2.keySet()) {
                                    if(map1Key.equals(map2Key)){
                                        Object value1 = map1.get(map1Key);
                                        Object value2 = map2.get(map2Key);
                                        if(!ObjectUtils.isEmpty(value1)){
                                            if(!value1.equals(value2)){
                                                storeCodes.add(map2Code);
                                                continue a;
                                            }
                                        }else if(!ObjectUtils.isEmpty(value2)){
                                            storeCodes.add(map2Code);
                                            continue a;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if(has){
                        storeCodes.add(map2Code);
                        continue a;
                    }
                }
            }
            if(djxq.containsKey(key)){
                Map<String, String> columnName = demandUpdateService.getColumnName(key, schemaCode);
                String name = columnName.get("name");
                String propertyType = columnName.get("propertyType");
                if(!ObjectUtils.isEmpty(djxqValue)){
                    if(!djxqValue.equals(djxqbgValue)){
                        //时间类型
                        if("DATE".equals(propertyType)){
                            bgMap.put("updateName",name);
                            if(djxqValue!=null){
                                bgMap.put("oldValue",djxqValue.toString().substring(0,djxqValue.toString().length()-11));
                            }
                            if(djxqbgValue!=null){
                                bgMap.put("nowValue",djxqbgValue.toString().substring(0,djxqValue.toString().length()-11));
                            }
                        } else if(userKeys.contains(key)){
                            //选人控件类型
                            if(ObjectUtils.isEmpty(djxqbgValue)){
                                List<SelectionValue> m1 = (List<SelectionValue>) djxqValue;
                                bgMap.put("updateName",name);
                                bgMap.put("oldValue",m1.get(0).getName());
                                bgMap.put("nowValue","");
                            }else{
                                List<SelectionValue> m1 = (List<SelectionValue>) djxqValue;
                                List<SelectionValue> m2 = (List<SelectionValue>) djxqbgValue;
                                bgMap.put("updateName",name);
                                bgMap.put("oldValue",m1.get(0).getName());
                                bgMap.put("nowValue",m2.get(0).getName());
                            }
                        } else if(selectKeys.contains(key)){
                            if(!djxqbgValue.equals(djxqValue)){
                                bgMap.put("updateName","关联项目");
                                bgMap.put("oldValue",djxqValue);
                                bgMap.put("nowValue",djxqbgValue);
                            }
                        } else {
                            if ("NUMERICAL".equals(propertyType)) {
                                bgMap.put("updateName", name);
                                if (djxqValue != null) {
                                    bgMap.put("oldValue", djxqValue.toString().substring(0, djxqValue.toString().indexOf(".")));
                                }
                                if (djxqbgValue != null) {
                                    bgMap.put("nowValue", djxqbgValue.toString().substring(0, djxqValue.toString().indexOf(".")));
                                }
                            } else {
                                bgMap.put("updateName", name);
                                bgMap.put("oldValue", djxqValue);
                                bgMap.put("nowValue", djxqbgValue);
                            }
                        }
                        save = true;
                    }
                }else if(!ObjectUtils.isEmpty(djxqbgValue)){
                    bgMap.put("updateName",name);
                    bgMap.put("oldValue",djxqValue);
                    bgMap.put("nowValue",djxqbgValue);
                    save = true;
                }
                if(save){
                    bgMap.put("id", UUID.randomUUID().toString().replaceAll("-",""));
                    bgMap.put("parentId",updateId);
                    bgMap.put("sortKey",sort);
                    bgMap.put("modifier",getUserId());
                    bgMap.put("modifiedTime",new Date());
                    sort = sort.add(BigDecimal.TEN);
                    bgMaps.add(bgMap);
                }
            }
        }
        if(!CollectionUtils.isEmpty(storeCodes)){
            Map<String, Object> bgMap = new HashMap<>();
            bgMap.put("updateName", "店检需求清单");
            bgMap.put("oldValue", "");
            bgMap.put("nowValue", String.join(",", storeCodes));
            bgMap.put("id", UUID.randomUUID().toString().replaceAll("-",""));
            bgMap.put("parentId",updateId);
            bgMap.put("sortKey",sort);
            bgMap.put("modifier",getUserId());
            bgMap.put("modifiedTime",new Date());
            bgMaps.add(bgMap);
        }
        if(!CollectionUtils.isEmpty(bgMaps)){
            demandUpdateService.addDjxqUpdateData(bgMaps);
        }
        return true;
    }

    @ApiOperation(value = "招聘需求变更留痕" )
    @GetMapping("/zpxqUpdate")
    public boolean zpxqUpdate(String id,String updateId){
        String schemaCode = "recruitmentDemand";
        BizObjectFacade bizObjectFacade = getBizObjectFacade();
        BizObjectCreatedModel zpxqData = bizObjectFacade.getBizObject("recruitmentDemand", id);
        BizObjectCreatedModel zpxqbgData = bizObjectFacade.getBizObject("recruitmentDemandUpdate", updateId);

        Map<String, Object> zpxq = zpxqData.getData();
        Map<String, Object> zpxqbg = zpxqbgData.getData();
        BigDecimal sort = BigDecimal.ZERO;

        List<Map<String, Object>> bgMaps = new ArrayList<>();
        //系统过滤字段
        List<String> sysKeys = Arrays.asList("modifiedTime","modifier","ownerDeptId","sequenceNo","sequenceStatus","createdTime","id","workflowInstanceId","owner","createdDeptId","ownerDeptQueryCode","name","creater");
        //过滤字段
        List<String> delKeys = Arrays.asList("copyDemand","projectName","storeAttachment");
        //隐藏字段
        List<String> hidKeys = Arrays.asList("state","isContract","LXCreateTime","persons","persons","zpxqId","finishedTime","subjectEndTime");
        //选人控件字段
        List<String> userKeys = Arrays.asList("operator","demandReceiver");
        //子表字段
        List<String> sheetKeys = Arrays.asList("demandDetailList");
        //关联单选的文本
        List<String> selectKeys = Arrays.asList("projectNameStr");

        //子表过滤字段
        List<String> sheetSysKey = Arrays.asList("id","parentId","modifier","sortKey","modifiedTime","rcsArea","sortKey","idCode","atsId","hasHiredNum");
        //需求数
        List<String> newDemandFor = Arrays.asList("demandFor");

        Set<String> storeCodes = new HashSet<>();
        for (String key : zpxqbg.keySet()) {
            Map<String, Object> bgMap = new HashMap<>();
            if(sysKeys.contains(key)||delKeys.contains(key)||hidKeys.contains(key)){
                continue;
            }
            boolean save = false;
            Object zpxqValue = zpxq.get(key);
            Object zpxqbgValue = zpxqbg.get(key);
            if(sheetKeys.contains(key)){
                //子表
                List<Map> m1 = (List<Map>) zpxq.get("demandDetails");
                List<Map> m2 = (List<Map>) zpxqbg.get("demandDetailList");
                //需求子表
                a : for (Map map2 : m2) {
                    String map2Code = map2.get("storeCode").toString();
                    boolean has = true;
                    //需求变更子表
                    for (Map map1 : m1) {
                        String map1Code = map1.get("storeCode").toString();
                        if(map1Code.equals(map2Code)){
                            has = false;
                            for (Object map1Key : map1.keySet()) {
                                if(sheetSysKey.contains(map1Key)){
                                    continue;
                                }
                                if(!newDemandFor.contains(map1Key)){
                                    Object demandFor1 = map1.get("demandFor");
                                    Object demandFor2 = map2.get("newDemandFor");
                                    if(demandFor1==null && demandFor2==null){
                                        continue;
                                    }
                                    if(demandFor1==null || demandFor2==null){
                                        storeCodes.add(map2Code);
                                        continue a;
                                    }
                                    if(!demandFor1.equals(demandFor2)){
                                        storeCodes.add(map2Code);
                                        continue a;
                                    }
                                }
                                for (Object map2Key : map2.keySet()) {
                                    if(map1Key.equals(map2Key)){
                                        Object value1 = map1.get(map1Key);
                                        Object value2 = map2.get(map2Key);
                                        if(!ObjectUtils.isEmpty(value1)){
                                            if(!value1.equals(value2)){
                                                storeCodes.add(map2Code);
                                                continue a;
                                            }
                                        }else if(!ObjectUtils.isEmpty(value2)){
                                            storeCodes.add(map2Code);
                                            continue a;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if(has){
                        storeCodes.add(map2Code);
                        continue a;
                    }
                }
            }
            if(zpxq.containsKey(key)){
                Map<String, String> columnName = demandUpdateService.getColumnName(key, schemaCode);
                String name = columnName.get("name");
                String propertyType = columnName.get("propertyType");
                if(!ObjectUtils.isEmpty(zpxqValue)){
                    if(!zpxqValue.equals(zpxqbgValue)){
                        //时间类型
                        if("DATE".equals(propertyType)){
                            bgMap.put("updateName",name);
                            if(zpxqValue!=null){
                                bgMap.put("oldValue",zpxqValue.toString().substring(0,zpxqValue.toString().length()-11));
                            }
                            if(zpxqbgValue!=null){
                                bgMap.put("nowValue",zpxqbgValue.toString().substring(0,zpxqValue.toString().length()-11));
                            }
                        } else if(userKeys.contains(key)){
                            //选人控件类型
                            if(ObjectUtils.isEmpty(zpxqbgValue)){
                                List<SelectionValue> m1 = (List<SelectionValue>) zpxqValue;
                                bgMap.put("updateName",name);
                                bgMap.put("oldValue",m1.get(0).getName());
                                bgMap.put("nowValue","");
                            }else{
                                List<SelectionValue> m1 = (List<SelectionValue>) zpxqValue;
                                List<SelectionValue> m2 = (List<SelectionValue>) zpxqbgValue;
                                bgMap.put("updateName",name);
                                bgMap.put("oldValue",m1.get(0).getName());
                                bgMap.put("nowValue",m2.get(0).getName());
                            }
                        } else if(selectKeys.contains(key)){
                            if(!zpxqbgValue.equals(zpxqValue)){
                                bgMap.put("updateName","关联项目");
                                bgMap.put("oldValue",zpxqValue);
                                bgMap.put("nowValue",zpxqbgValue);
                            }
                        } else {
                            if ("NUMERICAL".equals(propertyType)) {
                                bgMap.put("updateName", name);
                                if (zpxqValue != null) {
                                    bgMap.put("oldValue", zpxqValue.toString().substring(0, zpxqValue.toString().indexOf(".")));
                                }
                                if (zpxqbgValue != null) {
                                    bgMap.put("nowValue", zpxqbgValue.toString().substring(0, zpxqValue.toString().indexOf(".")));
                                }
                            } else {
                                bgMap.put("updateName", name);
                                bgMap.put("oldValue", zpxqValue);
                                bgMap.put("nowValue", zpxqbgValue);
                            }
                        }
                        save = true;
                    }
                }else if(!ObjectUtils.isEmpty(zpxqbgValue)){
                    bgMap.put("updateName",name);
                    bgMap.put("oldValue",zpxqValue);
                    bgMap.put("nowValue",zpxqbgValue);
                    save = true;
                }
                if(save){
                    bgMap.put("id", UUID.randomUUID().toString().replaceAll("-",""));
                    bgMap.put("parentId",updateId);
                    bgMap.put("sortKey",sort);
                    bgMap.put("modifier",getUserId());
                    bgMap.put("modifiedTime",new Date());
                    sort = sort.add(BigDecimal.TEN);
                    bgMaps.add(bgMap);
                }
            }
        }
        if(!CollectionUtils.isEmpty(storeCodes)){
            Map<String, Object> bgMap = new HashMap<>();
            bgMap.put("updateName", "招聘需求列表");
            bgMap.put("oldValue", "");
            bgMap.put("nowValue", String.join(",", storeCodes));
            bgMap.put("id", UUID.randomUUID().toString().replaceAll("-",""));
            bgMap.put("parentId",updateId);
            bgMap.put("sortKey",sort);
            bgMap.put("modifier",getUserId());
            bgMap.put("modifiedTime",new Date());
            bgMaps.add(bgMap);
        }
        if(!CollectionUtils.isEmpty(bgMaps)){
            demandUpdateService.addZpxqUpdateData(bgMaps);
        }
        return true;
    }


    @ApiOperation(value = "执行需求变更留痕" )
    @GetMapping("/zxxqUpdate")
    public boolean zxxqUpdate(String id,String updateId){
        String schemaCode = "ZXXQTB";
        BizObjectFacade bizObjectFacade = getBizObjectFacade();
        BizObjectCreatedModel zxxqData = bizObjectFacade.getBizObject("ZXXQTB", id);
        BizObjectCreatedModel zxxqbgData = bizObjectFacade.getBizObject("ZXXQBG", updateId);

        Map<String, Object> zxxq = zxxqData.getData();
        Map<String, Object> zxxqbg = zxxqbgData.getData();
        BigDecimal sort = BigDecimal.ZERO;

        List<Map<String, Object>> bgMaps = new ArrayList<>();
        //系统过滤字段
        List<String> sysKeys = Arrays.asList("modifiedTime","modifier","ownerDeptId","sequenceNo","sequenceStatus","createdTime","id","workflowInstanceId","owner","createdDeptId","ownerDeptQueryCode","name","creater");
        //过滤字段
        List<String> delKeys = Arrays.asList("copyDemand","projectName","attr");
        //隐藏字段
        List<String> hidKeys = Arrays.asList("projectNo","executeDemandId","persons","finishedTime","subjectEndTime");
        //选人控件字段
        List<String> userKeys = Arrays.asList("operator","demandReceiver");
        //子表字段
        List<String> sheetKeys = Arrays.asList("demandDetailList");
        //关联单选的文本
        List<String> selectKeys = Arrays.asList("projectNameStr");

        //子表过滤字段
        List<String> sheetSysKey = Arrays.asList("id","parentId","modifier","sortKey","modifiedTime","rcsArea","sortKey","idCode","atsId");
        //需求数
        List<String> storeNum = Arrays.asList("storeNum");
        List<String> planGearsNum = Arrays.asList("planGearsNum");
        List<String> planRoundNum = Arrays.asList("planRoundNum");

        Set<String> storeCodes = new HashSet<>();
        for (String key : zxxqbg.keySet()) {
            Map<String, Object> bgMap = new HashMap<>();
            if(sysKeys.contains(key)||delKeys.contains(key)||hidKeys.contains(key)){
                continue;
            }
            boolean save = false;
            Object zxxqValue = zxxq.get(key);
            Object zxxqbgValue = zxxqbg.get(key);
            if(sheetKeys.contains(key)){
                //子表
                List<Map> m1 = (List<Map>) zxxq.get("executeDetails");
                List<Map> m2 = (List<Map>) zxxqbg.get("executeDetailList");
                //需求子表
                a : for (Map map2 : m2) {
                    String map2Code = map2.get("sortKey").toString();
                    boolean has = true;
                    //需求变更子表
                    for (Map map1 : m1) {
                        String map1Code = map1.get("sortKey").toString();
                        if(map1Code.equals(map2Code)){
                            has = false;
                            for (Object map1Key : map1.keySet()) {
                                if(sheetSysKey.contains(map1Key)){
                                    continue;
                                }
                                if(storeNum.contains(map1Key)){
                                    Object demandFor1 = map1.get("storeNum");
                                    Object demandFor2 = map2.get("newStoreNum");
                                    if(!demandFor1.equals(demandFor2)){
                                        storeCodes.add(map2Code);
                                        continue a;
                                    }
                                }
                                if(planGearsNum.contains(map1Key)){
                                    Object demandFor1 = map1.get("planGearsNum");
                                    Object demandFor2 = map2.get("newPlanGearsNum");
                                    if(!demandFor1.equals(demandFor2)){
                                        storeCodes.add(map2Code);
                                        continue a;
                                    }
                                }
                                if(planRoundNum.contains(map1Key)){
                                    Object demandFor1 = map1.get("planRoundNum");
                                    Object demandFor2 = map2.get("newPlanRoundNum");
                                    if(!demandFor1.equals(demandFor2)){
                                        storeCodes.add(map2Code);
                                        continue a;
                                    }
                                }
                                for (Object map2Key : map2.keySet()) {
                                    if(map1Key.equals(map2Key)){
                                        Object value1 = map1.get(map1Key);
                                        Object value2 = map2.get(map2Key);
                                        if(!ObjectUtils.isEmpty(value1)){
                                            if(!value1.equals(value2)){
                                                storeCodes.add(map2Code);
                                                continue a;
                                            }
                                        }else if(!ObjectUtils.isEmpty(value2)){
                                            storeCodes.add(map2Code);
                                            continue a;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if(has){
                        storeCodes.add(map2Code);
                        continue a;
                    }
                }
            }
            if(zxxq.containsKey(key)){
                Map<String, String> columnName = demandUpdateService.getColumnName(key, schemaCode);
                String name = columnName.get("name");
                String propertyType = columnName.get("propertyType");
                if(!ObjectUtils.isEmpty(zxxqValue)){
                    if(!zxxqValue.equals(zxxqbgValue)){
                        //时间类型
                        if("DATE".equals(propertyType)){
                            bgMap.put("updateName",name);
                            if(zxxqValue!=null){
                                bgMap.put("oldValue",zxxqValue.toString().substring(0,zxxqValue.toString().length()-2));
                            }
                            if(zxxqbgValue!=null){
                                bgMap.put("nowValue",zxxqbgValue.toString().substring(0,zxxqValue.toString().length()-2));
                            }
                        } else if(userKeys.contains(key)){
                            //选人控件类型
                            if(ObjectUtils.isEmpty(zxxqbgValue)){
                                List<SelectionValue> m1 = (List<SelectionValue>) zxxqValue;
                                bgMap.put("updateName",name);
                                bgMap.put("oldValue",m1.get(0).getName());
                                bgMap.put("nowValue","");
                            }else{
                                List<SelectionValue> m1 = (List<SelectionValue>) zxxqValue;
                                List<SelectionValue> m2 = (List<SelectionValue>) zxxqbgValue;
                                bgMap.put("updateName",name);
                                bgMap.put("oldValue",m1.get(0).getName());
                                bgMap.put("nowValue",m2.get(0).getName());
                            }
                        } else if(selectKeys.contains(key)){
                            if(!zxxqbgValue.equals(zxxqValue)){
                                bgMap.put("updateName","关联项目");
                                bgMap.put("oldValue",zxxqValue);
                                bgMap.put("nowValue",zxxqbgValue);
                            }
                        } else {
                            if ("NUMERICAL".equals(propertyType)) {
                                bgMap.put("updateName", name);
                                if (zxxqValue != null) {
                                    bgMap.put("oldValue", zxxqValue.toString().substring(0, zxxqValue.toString().indexOf(".")));
                                }
                                if (zxxqbgValue != null) {
                                    bgMap.put("nowValue", zxxqbgValue.toString().substring(0, zxxqValue.toString().indexOf(".")));
                                }
                            } else {
                                bgMap.put("updateName", name);
                                bgMap.put("oldValue", zxxqValue);
                                bgMap.put("nowValue", zxxqbgValue);
                            }
                        }
                        save = true;
                    }
                }else if(!ObjectUtils.isEmpty(zxxqbgValue)){
                    bgMap.put("updateName",name);
                    bgMap.put("oldValue",zxxqValue);
                    bgMap.put("nowValue",zxxqbgValue);
                    save = true;
                }
                if(save){
                    bgMap.put("id", UUID.randomUUID().toString().replaceAll("-",""));
                    bgMap.put("parentId",updateId);
                    bgMap.put("sortKey",sort);
                    bgMap.put("modifier",getUserId());
                    bgMap.put("modifiedTime",new Date());
                    sort = sort.add(BigDecimal.TEN);
                    bgMaps.add(bgMap);
                }
            }
        }
        if(!CollectionUtils.isEmpty(storeCodes)){
            List<Integer> list = new ArrayList<>();
            for (String storeCode : storeCodes) {
                list.add((int) (Double.valueOf(storeCode)/10));
            }
            Map<String, Object> bgMap = new HashMap<>();
            bgMap.put("updateName", "执行需求列表");
            bgMap.put("oldValue", "");
            bgMap.put("nowValue", String.join(",", storeCodes));
            bgMap.put("id", UUID.randomUUID().toString().replaceAll("-",""));
            bgMap.put("parentId",updateId);
            bgMap.put("sortKey",sort);
            bgMap.put("modifier",getUserId());
            bgMap.put("modifiedTime",new Date());
            bgMaps.add(bgMap);
        }
        if(!CollectionUtils.isEmpty(bgMaps)){
            demandUpdateService.addZxxqUpdateData(bgMaps);
        }
        return true;
    }
}
